<template>
    <div>
        <h2>房号添加页面</h2>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>房型</td>
                    <td>
                        <select v-model="query.TypeId">
                            <option value="">请选择</option>
                            <option :value="item.TypeName" v-for="item in bb.list" :key="item.Id">{{item.TypeName}}</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td>房间号码</td>
                    <td>
                        <input type="text" v-model="query.NumName">
                    </td>
                </tr>

                <tr>
                    <td>房间描述</td>
                    <td>
                        <textarea cols="50" rows="5" v-model="query.Desc"></textarea>
                    </td>
                </tr>

                <tr>
                    <td>状态</td>
                    <td>
                        <input type="radio" name="state" :value="0" v-model="query.State">上架
                        <input type="radio" name="state" :value="1" v-model="query.State">下架
                    </td>
                </tr>

                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="添加" class="btn btn-primary" @click="add">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
import { useRoute, useRouter } from 'vue-router'
const router = useRouter();
const route = useRoute();
import moment from 'moment';

//下拉绑定
onMounted(() => {
    getBang();
})
let bb: any = reactive({
    list: []
})
const getBang = () => {
    axios({
        method: 'get',
        url: '/api/HouseType/ShowType',
        params: {
            pageIndex: 1,
            pageSize: 1000
        }
    })
        .then(res => {
            console.log(res);
            bb.list = res.data.list;
        })
        .catch(err => {
            console.log(err);
        })
}

let query=reactive({
  "TypeId": "",
  "NumName": '',
  "Desc": "",
  "State": '',
})

const add = () => {
    //有效性验证
    if(query.TypeId==""){
        alert('房型必选');
        return;
    }
    if(query.NumName==""){
        alert('房号不能为空');
        return;
    }
    if(query.Desc==""){
        alert('房号描述不能为空');
        return;
    }
    if(query.State===""){
        alert('状态不能为空');
        return;
    }

    //返回网络请求
    axios({
        method:'post',
        url:'/api/HouseType/AddNum',
        data:query
    })
    .then(res=>{
        console.log(res);
        if(res.data==-1){
            alert('房号不能重复');
            return;
        }
        if(res.data>0){
            alert('成功');
            router.push('/numShow');
        }
        else{
            alert('失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })

}
</script>

<style scoped></style>